package com.cqs.example.io.stream;

import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;
import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @Author lixw
 * @Date 7/22/20 11:49 PM
 */
@Slf4j
public class IOStreamState {

    static class  ScannerTask implements  Runnable {

        public void run() {
            Scanner scanner = new Scanner(System.in);
            //一直停留在这里 等待着输入
            while (scanner.hasNext()) {
                log.info(" 处理信息:{}", scanner.next());
            }
        }
    }

    static class  HighCpu implements  Runnable {

        public void run() {
            AtomicLong cnt = new AtomicLong();
            //一直停留在这里 等待着输入
            while (true) {
                if (cnt.addAndGet(1) % 10000000 == 0) {
                    log.info("hello word");
                    Thread.yield();
                }
                if (cnt.get() == Long.MAX_VALUE) {
                    cnt.set(0);
                }
            }
        }
    }



    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new ScannerTask(),"block-io");
        t.start();
        Thread t2 = new Thread(new HighCpu(),"high-cpu");
        t2.start();
        t.join();
    }
}
